/**
 * Copyright (c) 2013, Andrea Funto'. All rights reserved.
 * 
 * This file is part of the Dihedron Concurrent Utilities library ("Concurrent").
 *
 * "Commons" is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 *
 * "Concurrent" is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with "Concurrent". If not, see <http://www.gnu.org/licenses/>.
 */
package org.dihedron.concurrent;

import java.util.concurrent.Future;

import javax.annotation.ManagedBean;
import javax.ejb.EJB;

/**
 * Executes the given tasks as background operations, leveraging the application
 * server's asynchronous EBJ 3.1 methods facilities.
 * 
 * @author Andrea Funto'
 */
@ManagedBean
public class AsynchBeanExecutor<T> implements TaskExecutor<T> {

	/**
	 * The (injected) EJB 3.1 whose asynchronous method will be used to assign
	 * the task to a background thread. 
	 */
	@EJB AsynchBean<T> ejb;
	
	/**
	 * Assigns the given task to a worker thread in the JavaEE application server, 
	 * returning the task's Future object.
	 * 
	 *  @param task
	 *    the Task to be submitted for background execution to the application
	 *    server.
	 *  @return
	 *    the task's Future object, for result retrieval.
	 * @see 
	 *   org.dihedron.concurrent.TaskExecutor#submit(org.dihedron.concurrent.Task)
	 */
	@Override
	public Future<T> submit(Task<T> task) {
		try {
			return ejb.submit(task);
		} catch(Exception e) {
			
		}
		return null;
	}

}
